package com.mmp.main.servlet.report;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRParameter;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

public class ViewDeviceHistory extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String code = WebUtil.getParamValue(request, "code", null);
		String sn = WebUtil.getParamValue(request, "sn", null);
		float prevCost = WebUtil.getParamValueAsFloat(request, "prevCost", 0);

		/*
		 * HospitalDevice device = new HospitalDevice(); device.setCode(code);
		 * device.setSerialNo(sn);
		 * 
		 * JobOrder jo = new JobOrder(); jo.setHospitalDevice(device);
		 * 
		 * List<JobOrder> jOrders = dbm.find(jo);
		 * 
		 * if (jOrders == null || jOrders.isEmpty()) {
		 * announceErorrMessage(request, response,
		 * "لا يوجد أوامر عمل للجهاز المطلوب"); return; } else {
		 */

		List<JobOrder> list = getJobOrdersHistoryForDevice(code, sn);
		if (list == null || list.isEmpty()) {
			announceErorrMessage(request, response, "لا نتائج موافقة لبحثك");
			return;
		}
		
		HashMap params = new HashMap();
		params.put(Default.REPORT_TITLE_PARAM_NAME, Enums.REPORT_TITLE.DEVICE_HISTORY.getReportTitle());
		params.put("jOrderList", list);
		params.put("device", list.get(0).getHospitalDevice());
		params.put("prevCost", prevCost);
		params.put(JRParameter.REPORT_LOCALE, new Locale("ar"));
		viewPDFReport("rt4.jasper", null, params, response);
		// }
		/*
		 * HospitalDevice device = new DBQueryManager(dbm).getDevice(code, sn,
		 * null, true);
		 * 
		 * if (device == null) { announceErorrMessage(request, response,
		 * "لا يوجد جهاز موافق لبحثك"); return; }
		 * 
		 * String sql =
		 * "select sum(price) from job_order_spare_part where job_order_id in "
		 * + "(select id from job_order where device_id = " + device.getId() +
		 * ")";
		 * 
		 * List p = dbm.executeNativeQueryForSearch(sql, null); float totalCost
		 * = prevCost; if (p != null && !p.isEmpty() && p.get(0) != null)
		 * totalCost += (Double) p.get(0);
		 * 
		 * 
		 * 
		 * HashMap params = new HashMap(); // pass table datasource as parameter
		 * params.put("device", device); params.put("totalCost", totalCost);
		 * params.put(Default.REPORT_TITLE_PARAM_NAME,
		 * Enums.REPORT_TITLE.DEVICE_HISTORY.getReportTitle());
		 * 
		 * viewPDFReport("rt4.jasper", null, params, response);
		 */
	}

	private List<JobOrder> getJobOrdersHistoryForDevice(String deviceCode, String deviceSN) {

		if ((deviceCode == null || deviceCode.isEmpty()) && (deviceSN == null || deviceSN.isEmpty()))
			return null;

		String jpql = "select j from JobOrder j where j.id != null ";

		int i = 0;
		Map<Integer, Object> map = new HashMap<Integer, Object>();

		if (deviceCode != null && !deviceCode.trim().isEmpty()) {
			jpql += " and j.hospitalDevice.code = ? ";
			map.put(++i, deviceCode);
		}

		if (deviceSN != null && !deviceSN.trim().isEmpty()) {
			jpql += " and j.hospitalDevice.serialNo = ? ";
			map.put(++i, deviceSN);
		}

		jpql += " order by j.jobOrderDate";

		return dbm.queryJPQL(jpql, map, JobOrder.class);

	}
}
